import { ArticleDTO, CommentUserInfo, UserDTO } from '@/bean/dto';
import { RootState } from '.';
import {
  COMMENTUSERINFO,
  HOTLIST,
  SET_COMMENT_USER_INFO,
  SET_HOTLIST,
  SET_IS_MENU_VISIBLE,
  SET_USER_INFO,
  USERINFO,
} from './actiontype';

export default {
  [SET_IS_MENU_VISIBLE](state: RootState, payload: boolean): void {
    state.isMenuVisible = payload;
    if (payload) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = '';
    }
  },
  [SET_COMMENT_USER_INFO](state: RootState, payload: CommentUserInfo): void {
    if (payload) {
      state.commentUserInfo = payload;
      localStorage.setItem(COMMENTUSERINFO, JSON.stringify(payload));
    } else {
      state.commentUserInfo = null;
      localStorage.removeItem(COMMENTUSERINFO);
    }
  },
  [SET_HOTLIST](state: RootState, payload: ArticleDTO[]) {
    if (payload) {
      state.hotList = payload;
      sessionStorage.setItem(HOTLIST, JSON.stringify(payload));
    } else {
      state.hotList = null;
      sessionStorage.removeItem(HOTLIST);
    }
  },
  [SET_USER_INFO](state: RootState, payload: UserDTO): void {
    if (payload) {
      state.userInfo = payload;
      localStorage.setItem(USERINFO, JSON.stringify(payload));
    } else {
      state.userInfo = null;
      localStorage.removeItem(USERINFO);
    }
  },
};
